package com.erp.erp_project.controller;

import com.erp.erp_project.core.AjaxResult;
import com.erp.erp_project.core.annotation.Permission;
import com.erp.erp_project.mapper.EquipMapper;
import com.erp.erp_project.mapper.WorkMapper;
import com.erp.erp_project.model.Equip;
import com.erp.erp_project.model.Work;
import com.erp.erp_project.service.EquipService;
import com.erp.erp_project.service.WorkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping("work")
public class WorkController {
    private final WorkService workService;
    private final WorkMapper workMapper;
    private final EquipService equipService;
    private final EquipMapper equipMapper;

    //主页面
    @RequestMapping("work-home")
    public String work() {
        return "work/work-home";
    }

    //workList作业管理
    @GetMapping("list")
    public String workList(Model model) {
        return "work/list";
    }
    @PostMapping("list")
    @ResponseBody
    public AjaxResult<PageInfo<Work>> listAll(Work work, Integer page, Integer limit, Integer type, String va) {
        PageHelper.startPage(page, limit);
        Work wr = work;
        PageInfo<Work> pageInfo = new PageInfo<>(workService.list(work, type, va));
//        workService.list();
        return new AjaxResult<PageInfo<Work>>(pageInfo);
    }

    //添加
    @GetMapping("add")
    @Permission("作业添加")
    public String add(Model model) {
        Equip equip=new Equip();
        List<Equip> equips = equipMapper.list(equip);
        model.addAttribute("type",equips);
        return "work/add";
    }
    //添加
    @PostMapping("add")
    @ResponseBody
    @Permission("作业添加")
    public AjaxResult<String> add(Work work) {
        workService.add(work);
        return new AjaxResult<String>("添加成功");
    }

    //编辑
    @RequestMapping("edit")
    @Permission("作业修改")
    public String edit(Work work, Model model) {
        Equip equip=new Equip();
        List<Equip> equips = equipMapper.list(equip);
        model.addAttribute("work", workService.selectById(work.getId()));
        model.addAttribute("type",equips);
        return "work/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    @Permission("作业修改")
    public AjaxResult<String> edit(Work work) {
        workService.update(work);
        return new AjaxResult<String>("修改成功");
    }


    //删除
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    @Permission("作业删除")
    public AjaxResult<String> delete(@PathVariable Integer id) {
        workService.delete(id);
        return new AjaxResult<String>("删除成功");
    }

    //多选删除
    //多选删除
    //多选删除
    @DeleteMapping("/delAll/{ids}")
    @ResponseBody
    @Permission("作业删除")
    public AjaxResult<String> deleteAll(@PathVariable String ids) {
        String arr[] = ids.split(",");
        for (String i : arr) {
            workService.delete(Integer.parseInt(i));
        }
        return new AjaxResult<String>("删除成功");
    }
}
